
clear
set more off
capture log close
set seed 12345
set matsize 5000
set maxvar 20000	
* set globals
global OUTPUT "$DIR/Output"
global DATA "$DIR"	
cd "$DIR"

/* SET LOCALS */
* set earliest tax year to keep
local yrb = 2008
* set last tax year to keep
local yre = 2016
* set excluded base year for event-time and controls
local baseyr "2012"
* log or level ("ln" "level")
local rlog "ln"
local rloglab "`rlog'"


use $DATA/reg_file_firm.dta, clear

*******************************************
** regressions
*******************************************
local bm1 = `baseyr'-1
local bp1 = `baseyr'+1
local r1 "i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year i.cz_`baseyr'##ib`baseyr'.year"
local ddevents "`t' post dd"

***********************************************************************************************************
**  Heterogeneity [Fig 6]
***********************************************************************************************************

**********
** by number of owners
**********
gen owncat1 = n_own_2012==1
gen owncat2 = n_own_2012>1 
gen owncat3 = n_own_2012>1 & n_own_2012<10
gen owncat4 = n_own_2012>=10

local la1 "single"
local la2 "mult"
local la3 "o2_10"
local la4 "g10"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 1 {
	foreach cc in 1 2 3 4 {
		eststo `la`cc''_r`r': quietly reghdfe `rlog'`o' `ddevents'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) &  owncat`cc'==1 & year>=2011, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	}
	esttab using "$OUTPUT/figure6_nowners.csv", replace se ar2 mtitles( ) keep(dd) 
	eststo clear
}


*******************
**  By firm size
*******************
gen fsize1 = n_ft_lower_2012<15
gen fsize2 = n_ft_lower_2012>=15 & n_ft_lower_2012<50
gen fsize3 = n_ft_lower_2012>=50 & n_ft_lower_2012<100
gen fsize4 = n_ft_lower_2012>=100 

local la1 "u15"
local la2 "fs15_50"
local la3 "fs50_100"
local la4 "o100"

foreach o in wages_ft {
	* loop over regressions
	foreach r in 1 {
	foreach cc in 1 2 3 4 {
		eststo `la`cc''_r`r': quietly reghdfe `rlog'`o' `ddevents'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) &  fsize`cc'==1 & year>=2011, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	}
	esttab using "$OUTPUT/figure6_fsize.csv", replace se ar2 mtitles( ) keep(dd) 
	eststo clear
}


******************
**  By valadd and revenue
******************
foreach i in valadd_2012 revenue_2012  {
	xtile `i'cat3 = `i',nquantiles(3)
}
replace valadd_2012cat3 = 0 if valadd_2012<0

/* valadd */
foreach o in wages_ft {
	* loop over regressions
	foreach r in 1 {
	foreach cc in 1 2 3  {
		eststo va`cc'_r`r': quietly reghdfe `rlog'`o' `ddevents'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) &  valadd_2012cat3==`cc' & year>=2011, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	}
	esttab using "$OUTPUT/figure6_valadd.csv", replace se ar2 mtitles( ) keep(dd)
	eststo clear
}

/* revenue */
foreach o in wages_ft {
	* loop over regressions
	foreach r in 1 {
	foreach cc in 1 2 3  {
		eststo va`cc'_r`r': quietly reghdfe `rlog'`o' `ddevents'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) &  revenue_2012cat3==`cc' & year>=2011, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	}
	esttab using "$OUTPUT/figure6_revenue.csv", replace se ar2 mtitles( ) keep(dd) 
	eststo clear
}


******************
**  by profitibility 
*****************
gen profitm = netinc_2012/revenue_2012

sum profitm if year==2012,d
gen profitcat = .
replace profitcat = 1 if profitm<=r(p25)
replace profitcat = 2 if profitm>r(p25) & profitm<=r(p50)
replace profitcat = 3 if profitm>r(p50) & profitm<=r(p75)
replace profitcat = 4 if profitm>r(p75) & profitm~=.

* make it so cat 1 is losses
gen loss=netinc_2012<0
replace profitcat = 2 if profitcat==1 & netinc_2012>0

foreach o in wages_ft {
	* loop over regressions
	foreach r in 1 {
	foreach cc in 1 2 3 4 {
		eststo va`cc'_r`r': quietly reghdfe `rlog'`o' `ddevents'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) & profitcat==`cc' & year>=2011, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	}
	esttab using "$OUTPUT/figure6_profitmarg.csv", replace se ar2 mtitles( ) keep(dd)
	eststo clear
}


******************
**  by Labor Share 
******************
foreach v in tot_wages {
	gen vv = 0
	replace vv = `v' if year==2012
	bys firm_tin: egen `v'_2012 =sum(vv)
	drop vv
}
gen labshare = tot_wages_2012/revenue_2012

foreach i in labshare  {
	xtile `i'cat3 = `i',nquantiles(3)
}

foreach o in wages_ft {
	* loop over regressions
	foreach r in 1 {
	foreach cc in 1 2 3  {
		eststo va`cc'_r`r': quietly reghdfe `rlog'`o' `ddevents'  [aw=n_ft_lower_2012] if (`t'==1 | `t'==0) & labsharecat3==`cc' & year>=2011, absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	}
	esttab using "$OUTPUT/figure6_labshare.csv", replace se ar2 mtitles( ) keep(dd)
	eststo clear
}




clear
exit

